﻿//-----------------------------------------------------------------------
// <copyright file="MessageHandlerInstance.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Base.Message
{
    using System;
    using TileTool.Message;

    /// <summary>
    /// Instance of a message handler
    /// </summary>
    internal class MessageHandlerInstance
    {
        #region Constructor

        /// <summary>
        /// Construct the handler instance from a channel and a caller.
        /// </summary>
        /// <param name="id">ID for this handler instance.</param>
        /// <param name="priority">Message handler priority.</param>
        /// <param name="channel">Message channel we are listening on.</param>
        /// <param name="filter">Filter to use for message filtering.</param>
        /// <param name="caller">Caller to call when we get a message.</param>
        public MessageHandlerInstance(ulong id, int priority, MessageChannel channel, IMessageFilter filter, IHandlerCaller caller)
        {
            this.ID = id;
            this.Channel = channel;
            this.Caller = caller;
            this.Priority = priority;
            this.Filter = filter;
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets the ID of this registration.
        /// </summary>
        public ulong ID
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the priority of the handler.
        /// </summary>
        public int Priority
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the message channel to listen on.
        /// </summary>
        public MessageChannel Channel
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the caller to call when a message arrives.
        /// </summary>
        public IHandlerCaller Caller
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the filter to use for message filtering. Can be null.
        /// </summary>
        public IMessageFilter Filter
        {
            get;
            private set;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Equals overload.
        /// </summary>
        /// <param name="obj">Object to test for equality.</param>
        /// <returns>Returns true if this object is equal to the passed object.</returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            if (GetType() != obj.GetType())
            {
                return false;
            }

            MessageHandlerInstance rhs = (MessageHandlerInstance)obj;

            if (this.Channel != rhs.Channel)
            {
                return false;
            }

            // $TODO - Why doesn't normal != work?
            if (!this.Caller.Equals(rhs.Caller))
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Compute a hash code for this object.
        /// </summary>
        /// <returns>Returns the hash code for the object.</returns>
        public override int GetHashCode()
        {
            return this.Channel.GetHashCode() ^ this.Caller.GetHashCode();
        }

        #endregion
    }
}
